/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jboss.seam.scxml.impl.context;

import java.lang.annotation.Annotation;
import org.apache.commons.scxml.Context;
import org.apache.commons.scxml.SCXMLExecutor;
import org.jboss.seam.scxml.DialogScoped;
import org.jboss.seam.scxml.impl.DialogUtils;
import org.jboss.weld.context.AbstractContext;
import org.jboss.weld.context.beanstore.BeanStore;
import org.jboss.weld.context.beanstore.HashMapBeanStore;

/**
 *
 * @author Waldemar Kłaczyński
 */
public class DialogContextImpl extends AbstractContext {

    public DialogContextImpl() {
        super(true);
    }

    private SCXMLExecutor getExecutor() {
        return DialogUtils.getExecutor();
    }

    private BeanStore getStore() {
        SCXMLExecutor executor = getExecutor();
        if (executor == null) {
            return null;
        }
        Context context = executor.getRootContext();
        BeanStore instance = (BeanStore) context.get("_____@@@SopeDialogContext____");
        if (instance == null) {
            instance = new HashMapBeanStore();
            context.set("_____@@@SopeDialogContext____", instance);
        }
        return instance;
    }

    @Override
    public boolean isActive() {
        return getStore() != null;
    }

    @Override
    protected BeanStore getBeanStore() {
        return getStore();
    }

    @Override
    public Class<? extends Annotation> getScope() {
        return DialogScoped.class;
    }

}
